const AsyncMysqljs = require('../mysql/index')
const Query = AsyncMysqljs().query

module.exports = async (ctx, next) => {
    let {type, is_show, name, pic_url, down_url, desc, baidu_url, baidu_password} = ctx.request.body
    const bodyData = ctx.request.body
    const base = {type, is_show, name, pic_url, desc}
    for(item in base) {
        if(typeof base[item] === 'undefined') {
            errorMsg(ctx, `${item} 不能为空！`)
            return
        }
    }
    if(!down_url) down_url = ''
    if(!baidu_url) baidu_url = ''
    if(!baidu_password) baidu_password = ''

    const sql = 'INSERT INTO film(type, is_show, name, pic_url, down_url, desc_txt, baidu_url, baidu_password) VALUES(?, ?, ?, ?, ?, ?, ?, ?)'
    try {
        const film = await Query(sql, [type, is_show, name, pic_url, down_url, desc, baidu_url, baidu_password])
    } catch (error) {
        errorMsg(ctx, error)
        return
    }
    
    
    ctx.body = {
        code: 1,
        data: {},
        msg: '添加成功！'
    }
    
}
const errorMsg = (ctx, msg) => {
    ctx.body = {
        code: 0,
        data: {},
        msg: msg
    }
}